#-------------------------------------------------------------------------------
# Name:        Version Check
# Purpose:
#
# Author:      Pablo
#
# Created:     09/01/2013
# Copyright:   (c) Monxcleyr Productions 2013
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import urllib2, io, logging
from ..game import config

class VersionCheck():

  """Will check current game version against website and prompt user to update, if needed"""

  def __init__(self):
    try:
      self.versionconfig = urllib2.urlopen('http://monxcleyr.tk/versions/versions.ini')
##      print self.versionconfig.read()
    except:
      logging.warning('Syncing to Monxcleyr Version Checking service failed')
      self.versionconfig = None

    self.conf = config.Config()
    if self.versionconfig != None:
      try:
        self.conf.readfp(io.BytesIO(self.versionconfig.read()))
      except:
        logging.warning('Syncing to Monxcleyr Version Checking service failed')
        self.versionconfig = None



  def check(self, name, version):
    if self.versionconfig == None:
      return None
    confversion = self.conf.get(name, 'version')
    if confversion == version:
      logging.info('%s is up-to-date.' % name)
      return 1
    else:
      logging.info('There is a new version of %s avaliable. Download at %s' % (name, self.conf.get(name, 'link')))
      return 0


